﻿var language = $("meta[http-equiv=Content-Language]").attr("content");
if (!language) {
    var language = $("meta[httpEquiv=Content-Language]").attr("content");
}

$(document).ready(function () {
    // Agreement
    $("#agreement").attr("checked", false);
    $("#submit_form").attr("disabled", true);
    $("#agreement").click(function () {
        if ($(this).attr("checked")) {
            $("#submit_form").attr("disabled", false);
            return;
        }
        else {
            $("#submit_form").attr("disabled", true);
            return;
        }
    });

    // Form Validation
    $("form").validate({
        rules: {
            topic: { required: true },
            name: { required: true },
            age: { digits: true, rangelength: [1, 3] },
            parent: { required: function () { return ($("#age").val() < 15) && ($("#age").val() != "") } },
            tel: { required: true, digits: true, rangelength: [8, 8] },
            email: { required: true, email: true }
        },
        errorPlacement: function (error, element) {
            if (element.attr("name") == "topic") {
                error.appendTo("#errormsg");
            }
            else {
                error.appendTo(element.next("br").next("label"));
            }
        },
        messages: {
            topic: { required: getErrorMsg("NOTOPIC", language) },
            name: { required: getErrorMsg("NONAME", language) },
            age: {
                digits: getErrorMsg("NOTDIGIT", language),
                rangelength: getErrorMsg("NOTCORRECTAGE", language)
            },
            parent: { required: getErrorMsg("NOPARENT", language) },
            tel: {
                required: getErrorMsg("NOTEL", language),
                digits: getErrorMsg("NOTDIGIT", language),
                rangelength: getErrorMsg("NOTEIGHT", language)
            },
            email: {
                required: getErrorMsg("NOEMAIL", language),
                email: getErrorMsg("NOTCORRECTEMAIL", language)
            }
        }
    });

});

function getLanguage(language) {
    switch (language) {
        case "zh-tw":
            return "chi";
            break;
        case "zh-cn":
            return "chis";
            break;
        case "en-gb":
            return "eng";
            break;
        case "pt":
            return "por";
            break;
        default:
            return "chi";
            break;
    }
}

function getErrorMsg(Desc, lang) {
    var language = getLanguage(lang);
    switch (Desc) {
        case "NOTOPIC":
            switch (language) {
                case "chi":
                    return "請選擇您想參與的講座";
                    break;
                case "chis":
                    return "请选择您想参与的讲座";
                    break;
                case "por":
                    return "Escolha o tópico"
                    break;
                case "eng":
                    return "Please select topic"
                    break;
            }
            break;
        case "NONAME":
            switch (language) {
                case "chi":
                    return "請填寫姓名";
                    break;
                case "chis":
                    return "请填写姓名"
                    break;
                case "por":
                    return "Preencha o seu nome"
                    break;
                case "eng":
                    return "Please fill in name"
                    break;
            }
            break;
        case "NOTCORRECTAGE":
            switch (language) {
                case "chi":
                    return "請填寫正確的年齡";
                    break;
                case "chis":
                    return "请填写正确的年龄"
                    break;
                case "por":
                    return "Preencha um endereço de idade correcto"
                    break;
                case "eng":
                    return "Please fill in correct age"
                    break;
            }
            break;
        case "NOPARENT":
            switch (language) {
                case "chi":
                    return "請填寫家長姓名";
                    break;
                case "chis":
                    return "请填写家长姓名"
                    break;
                case "por":
                    return "Preencha o seu filação"
                    break;
                case "eng":
                    return "Please fill in parent's name"
                    break;
            }
            break;
        case "NOTEL":
            switch (language) {
                case "chi":
                    return "請填寫電話";
                    break;
                case "chis":
                    return "请填写电话"
                    break;
                case "por":
                    return "Preencha o seu número de telefone"
                    break;
                case "eng":
                    return "Please fill in telephone number"
                    break;
            }
            break;
        case "NOTDIGIT":
            switch (language) {
                case "chi":
                    return "請填寫數字";
                    break;
                case "chis":
                    return "请填写数字"
                    break;
                case "por":
                    return "Preencha o seu dígitos"
                    break;
                case "eng":
                    return "Please fill in digits"
                    break;
            }
            break;
        case "NOTEIGHT":
            switch (language) {
                case "chi":
                    return "電話號碼須為8位";
                    break;
                case "chis":
                    return "电话号码须为8位"
                    break;
                case "por":
                    return "O número de dígitos do número de telefone deve ser 8"
                    break;
                case "eng":
                    return "Telephone number should be 8 digits"
                    break;
            }
            break;
        case "NOEMAIL":
            switch (language) {
                case "chi":
                    return "請填寫電郵";
                    break;
                case "chis":
                    return "请填写电邮"
                    break;
                case "por":
                    return "Preencha o endereço de e-mail"
                    break;
                case "eng":
                    return "Please fill in email"
                    break;
            }
            break;
        case "NOTCORRECTEMAIL":
            switch (language) {
                case "chi":
                    return "請填寫正確的電郵";
                    break;
                case "chis":
                    return "请填写正确的电邮"
                    break;
                case "por":
                    return "Preencha um endereço de e-mail correcto"
                    break;
                case "eng":
                    return "Please fill in correct email"
                    break;
            }
            break;
    }
}